import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/regional_organization_tree/regional_organization_tree_view.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/common/risk_tree_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_security_risk_grading_control_list_app_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_task_practicable_status_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_control_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///风险管控清单
class TaskImplementationController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选后的标题
  String? title = '全部';

  /// 选中的节点
  RiskTreeEntity selectedEntity = RiskTreeEntity();

  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  GetTaskPracticableStatusEntity? getTaskPracticableStatusEntity;

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  /// 筛选按钮
  void onFilterButtonPressed() {
    final RiskControlController controller = Get.find<RiskControlController>();
    controller.filterWidget.value = _filterWidget();
    controller.riskRightScaffoldKey.currentState?.openEndDrawer();
    // riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  // 获取任务数据
  Future<List<GetSecurityRiskGradingControlListAppEntity>> getTaskData(int index, int size, {String? search}) async {
    queryParameters['pageNum'] = index;
    queryParameters['pageSize'] = size;
    queryParameters['id'] = selectedEntity.id; //当前层级的id、对象 单元 事件 措施的id 默认全部 // 需要筛选后的id填入
    queryParameters['level'] = selectedEntity.level; //默认 1
    List<GetSecurityRiskGradingControlListAppEntity>? data = [];
    final res = await HttpUtil()
        .generateGetRequest<List<GetSecurityRiskGradingControlListAppEntity>>(
          Interface.getSecurityRiskGradingControlList,
          queryParameters: queryParameters,
        )
        .execute();
    res.onSuccess((result) {
      data = result.data;
    });
    return data ?? [];
  }

  /// 风险管控清单详情 措施详情
  void onTaskDetailButtonPressed(GetSecurityRiskGradingControlListAppEntity item) {
    Get.toNamed(AppRoutes.taskDetails, arguments: item.id);
  }

  ///任务详情  Get.toNamed(AppRoutes.taskPracticable);
  void onTaskButtonPressed() {
    AppToast.show('暂无~');
    return;
  }

  @override
  void onInit() {
    super.onInit();
    _getTaskPracticableStatus();
  }

  ///获取事件任务落实情况(任务落实统计框）
  void _getTaskPracticableStatus() async {
    final res = await HttpUtil().generateGetRequest<GetTaskPracticableStatusEntity>(
      Interface.getPracticableStatus,
      queryParameters: {'id': selectedEntity.id, 'level': selectedEntity.level}, // 需要筛选后的id填入 对象的id 单元的id 事件的id 措施的id 默认这写的事件的id
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        getTaskPracticableStatusEntity = result.data;
      }
    });
    update();
  }

  /// 重置
  void reset() {
    final RiskControlController controller = Get.find<RiskControlController>();
    controller.riskRightScaffoldKey.currentState?.closeEndDrawer();
    selectedEntity = RiskTreeEntity();
    globalKey.currentState!.onRefresh();
    title = '全部';
    update();
  }

  /// 确定
  void confirm() {
    Get.back();
    if (selectedEntity.level == 1) {
      title = '全部';
    } else if (selectedEntity.level == 2) {
      title = '全部/${selectedEntity.name}';
    } else {
      title = '${selectedEntity.path}/${selectedEntity.name}';
      if (title != null && title!.startsWith('/')) {
        title = title?.substring(1, title?.length);
        title = '全部/$title';
      }
    }
    globalKey.currentState!.onRefresh();
    update();
  }

  Widget _filterWidget() {
    return CustomEndDrawer(
      centerContext: Column(
        children: [
          RegionalOrganizationTreePage(
            selectedEntity: selectedEntity,
            onCallBack: (RiskTreeEntity entity) {
              selectedEntity = entity;
            },
          ),
        ],
      ),
      reset: reset,
      confirm: confirm,
    );
  }
}
